package com.example.shopping.api;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by doanht on 12/5/13.
 */
public class SHShoppingAPI {
    protected SHParserDataManager parserDataManager;

    public SHShoppingAPI(SHParserDataManager parserDataManager){
        super();
        this.parserDataManager = parserDataManager;
    }

    protected JsonHttpResponseHandler baseResponseHandler(final SHResponseInterface responseInterface, final SHParserData parserData){
        return new JsonHttpResponseHandler() {

            public void onSuccess(int statusCode, org.apache.http.Header[] headers, java.lang.String responseBody){
                super.onSuccess(statusCode,headers,responseBody);
            }



            @Override
            public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable err) {
                super.onFailure(statusCode, headers, responseBody, err);

                if (responseInterface != null) {
                    responseInterface.responseDataFail(this, statusCode, err, responseBody);
                }
            }

            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
            }

            @Override
            public void onSuccess(int statusCode, JSONObject response){
                super.onSuccess(statusCode,response);
                if (responseInterface != null && parserData != null) {
                    try {
                        SHResponseData responseData = parserData.parseData(response);
                        responseInterface.responseData(this, statusCode, responseData);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        responseInterface.responseData(this, statusCode, null);
                    }
                }
            }
//
//            @Override
//            public void onFailure(int statusCode, Throwable err, JSONObject errorResponse){
//                if (responseInterface != null) {
//                    responseInterface.responseDataFail(this, statusCode, err, errorResponse);
//                }
//            }
        };
    }

    public void searchItem(String item, SHResponseInterface responseInterface) {
        RequestParams params = new RequestParams();
        params.add("itemcd", item);
        searchItem(params, responseInterface);
    }

    public void searchItem(RequestParams params, SHResponseInterface responseInterface) {
        SHParserData parserData = this.parserDataManager.getParseData("SearchParser");
        SHRestApi.get("search.php", params,baseResponseHandler(responseInterface, parserData));
    }

    public void saveItem(String itemID, String itemCount, SHResponseInterface responseInterface) {
        RequestParams params = new RequestParams();
        params.add("itemcd",itemID);
        params.add("quantity",itemCount);
        saveItem(params,responseInterface);
    }

    public void saveItem(RequestParams params, SHResponseInterface responseInterface) {
        SHParserData parserData = this.parserDataManager.getParseData("SaveParser");
        SHRestApi.get("update.php", params,baseResponseHandler(responseInterface, parserData));
    }

}